home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / pascal / bbskt30a.zip / MTASK20.ZIP / MTASK.DOC next >
Text File  |  1990-03-12  |  18KB  |  526 lines

  1. .dhMTASK 2.0 by Wayne Conrad
  2. .np Documentation written using Multi-Edit 4.01Pd
  3. .rm 72
  4. .df .ce -.pa-
  5. .tm6
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12. .ceMTASK
  13.  
  14.  
  15. MTASK is a unit for Turbo Pascal 5.5, to allow a Turbo Pascal program
  16. to exhibit simple multi-tasking.  MTASK gives your program a
  17. non-preemptive, request driven multi-tasking capability.  I will
  18. explain what I mean by that later.
  19.  
  20. MTASK 2.0 was written and donated to the public domain by Wayne E.
  21. Conrad (me) in March of 1990.  I may be contact via my BBS,
  22.  
  23.      Pascalaholics Anonymous
  24.      (602) 484-9356
  25.      300/1200/2400 bps
  26.      24 hours/day
  27.  
  28. or by mail at my home:
  29.  
  30.      10 E Bell Road #1001
  31.      Phoenix, AZ  85022
  32.  
  33. I am interested in any modifications, bug reports, or comments you
  34. have.
  35.  
  36. If you modify this package, please keep my name and the name of any
  37. other programmers who've worked on it intact.  Give credit to yourself,
  38. too!  Please distribute the complete package, with documentation and
  39. demonstration programs included.
  40.  
  41.  
  42. 1.1 INTRODUCTION
  43.  
  44.  
  45. MTASK allows your Turbo Pascal program to do simple multi-tasking.  I
  46. call MTASK's brand of multi-tasking "non-preemptive, request driven."
  47.  
  48. Preemptive means that the switch from one task to another can happen at
  49. almost any time.  Most preemptive systems have an interrupt driver
  50. hooked to a hardware timer, which causes a task switch every time the
  51. hardware timer goes off.  The advantage of this kind of multi-tasking
  52. is that your programs don't have to be written with multi-tasking in
  53. mind, and don't even have to know that its taking place.  Also, no
  54. program can hog the system for long, because the interrupt driver
  55. switches from one program to another fairly often.  Desqview and
  56. Double-DOS, and OS/2 are operating environments that do preemptive,
  57. interrupt-driven multi-tasking.  The disadvantage of this kind of
  58. multi-tasking is that it can be complex to write and difficult in the
  59. extreme to debug.  These difficulties are compounded in an MS-DOS
  60. environment because MS-DOS was not meant to be used in a multi-tasking
  61. environment.
  62.  
  63. On the other hand, non-preemptive multi-tasking only switches tasks at
  64. certain, well defined times.  There is no interrupt driver that forces
  65. task switches.  In the original Macintosh operating system, for example,
  66. task switches only occured when a task called the operating system.  The
  67. advantage of non-preemptive multi-tasking is that it is much simpler to
  68. write and debug than preemptive multi-tasking, because the system has
  69. total control of when task-switches occur.  The disadvantage to this
  70. form of multi-tasking is that a task must request a task switch often if
  71. the other tasks are to receive their chance to execute.  If a task does
  72. not request a task switch for a long time, the other tasks will appear
  73. to pause.  What's worse, if a task crashes, it won't be able to call the
  74. operating system to let the other tasks execute, so they'll all be hung
  75. too.
  76.  
  77. MTASK implements a very simple method of non-preemptive multi-tasking
  78. that I call "request driven."  Request driven means that task switches
  79. occur only when the current task calls MTASK and requests a switch.
  80. (The sole exception is that a task switch occurs when the current task
  81. terminates itself).  This is about the simplest form of multi-tasking
  82. that I can envision.  It is so simple that the entire MTASK unit
  83. compiles to only about 1400 bytes with stack checking and range
  84. checking turned off, or less if you don't use all of its procedures.
  85. This simplicity also made MTASK easy to write and debug.  MTASK was
  86. written in one day!
  87.  
  88.  
  89.  
  90. 1.2 WHAT ARE MTASK'S LIMITS?
  91.  
  92.  
  93. MTASK allows your program to set up multiple tasks within itself.
  94. These tasks will execute concurrently.  However, it does not effect
  95. anything outside of your program.  It does not allow you to run
  96. multiple programs, multiple copies of COMMAND.COM, or anything else
  97. like that.  It simply allows your program to do several things
  98. concurrently without stumbling over itself.
  99.  
  100. As far as DOS is concerned, your program using MTASK is still just a
  101. simple program.  All of the gymnastics to keep track of multiple tasks
  102. are done by MTASK, withing your program, without the knowledge or
  103. consent of DOS or anything else outside of your program.  Because MTASK
  104. is so simple, it will coexist fine with any "real" multi-tasking DOS
  105. you have set up, such as DesqView or Double-DOS.  Whenever the DOS
  106. gives your program some time, your program will dole out that time to
  107. its tasks.
  108.  
  109. Your program must continue to execute for its tasks to execute.  If any
  110. task in your program exits to DOS for any reason, including a run-time
  111. error, all tasks stop executing.  If one of your tasks shells out by
  112. using Turbo's Exec function, then the other tasks in your program are
  113. suspended until control returns from the Exec function to your program.
  114.  
  115. MTASK must not be made into an overlay.  Any of the tasks it controls
  116. may be overlays, although that may be unwise.  You could end up loading
  117. an overlay from disk during each task switch!
  118.  
  119.  
  120. 2.1 SUMMARY OF PROCEDURES AND FUNCTIONS
  121.  
  122.  
  123. To use MTASK, include it in your program's USES statements.  MTASK will
  124. initialize itself automatically, making your main program task #1.
  125. Your program can then use the following procedures and functions to
  126. create and control tasks:
  127.  
  128.  
  129.      create_task         Create another task
  130.  
  131.      terminate_task      Destroy a task
  132.  
  133.     switch_task        Switch to another task
  134.  
  135.     current_task_id    Return the task ID of the current task
  136.  
  137.     number_of_tasks    Return the current number of tasks
  138.  
  139.  
  140. 2.1.1 PROCEDURE CREATE_TASK
  141.  
  142.  
  143. PROCEDURE create_task
  144.   (
  145.   task      : task_proc;
  146.   VAR param ;
  147.   stack_size: Word;
  148.   VAR id    : Word;
  149.   VAR result: Word
  150.   );
  151.  
  152.  
  153.      TASK is the procedure to make into a task.  It must match type
  154.      task_proc, having a single variable as its parameter.
  155.  
  156.      PARAM is the parameter to pass to new_task.  It may be a variable
  157.      of any type, so long its what the task expects.  For example, if
  158.      you pass a Word and the task expects a LongInt, the task will get
  159.      invalid data.
  160.  
  161.      STACK_SIZE is the size of the new task's stack.  A stack will be
  162.      allocated from the heap.  The minimum stack size is 512 bytes, but
  163.      most tasks will need more.
  164.  
  165.      ID is set to the task ID of the newly created task.  If the task
  166.      is not created because of an error, then id is not set.
  167.  
  168.     RESULT is the result code, which is set to one of these values:
  169.  
  170.         0                No error, task created ok
  171.  
  172.           heap_full           Unable to allocate heap for the task's
  173.                               stack
  174.  
  175.           too_many_tasks      Maximum number of tasks are already
  176.                               running
  177.  
  178.  
  179. The new task is created and added to the end of the task list.  The new
  180. task will be executed when the task before it calls switch_task.
  181.  
  182.  
  183. 2.1.2 PROCEDURE TERMINATE_TASK
  184.  
  185.  
  186. PROCEDURE terminate_task (id: Word; VAR result: Word);
  187.  
  188.  
  189.      ID is the task id of the task you want to terminate.  If ID = 0,
  190.      then the current task will be terminated.
  191.  
  192.     RESULT is the result code, which is set to one of these values.
  193.  
  194.           0                   No error, task deleted ok
  195.  
  196.         invalid_task_id    There is no task with that ID number
  197.  
  198.  
  199. The designated task will be removed from the task list.  If its stack
  200. was allocated from the heap, it is returned to the heap.
  201.  
  202. If the terminated task is the current task and there is another task in
  203. the task list, a task switch occurs.  On the other hand, if the
  204. terminated task is the current task and there are no other tasks in the
  205. task list, then the program exits to DOS.
  206.  
  207. A task may terminate itself by returning from its main procedure.  For
  208. example, when this task is executed, it will immediately display a
  209. message and then terminate itself.
  210.  
  211.  
  212.      PROCEDURE task (VAR param);
  213.     BEGIN
  214.        Writeln ('We just started, but already we're terminating')
  215.     END;
  216.  
  217.  
  218. 2.1.3 PROCEDURE switch_task
  219.  
  220.  
  221.     PROCEDURE switch_task;
  222.  
  223.  
  224. This procedure causes an immediate switch to the next task in the task
  225. list.  The